package org.hao.samples.controller;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.hao.samples.model.Student;
import org.hao.samples.model.StudentVO;
import org.hao.samples.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {

	@Autowired
	private StudentService studentService;

	// 进入登录页面
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLogin(Model model) {
		StudentVO studentvo = new StudentVO();
		model.addAttribute("studentvo", studentvo);
		return "login/login";
	}

	// 进行登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(RedirectAttributes redirectAttributes, StudentVO vo, HttpSession session) {
		if (vo != null) {
			Student resultVO = studentService.getUserById(vo.getUser_id());
			if (resultVO != null) {
				if (!vo.getPassword().isEmpty()) {
					String encodePassword = vo.getPassword();
					Date currentTime = null;
					Calendar currentTimecalendar = Calendar.getInstance();
					currentTimecalendar.setTime(new Date());
					currentTimecalendar.add(Calendar.MINUTE, -5);
					currentTime = currentTimecalendar.getTime();
					System.out.println(currentTime + "----------" + new Date() + "----------" + currentTimecalendar);
					if (resultVO.getLast_lock_time().before(currentTime)) {
						System.out.println("不在锁定时间范围内");
						if (encodePassword.equals(resultVO.getPassword())) {
							resultVO.setLogin_fail_count(0);
							try {
								studentService.editStudent(resultVO);
							} catch (Exception e) {
								e.printStackTrace();
							}
							session.setAttribute("examUser", resultVO);
							session.setAttribute("role", studentService.findRoleById(vo.getUser_id()).getRoleId());
							return "redirect:/index";
						} else {
							resultVO.setLogin_fail_count(resultVO.getLogin_fail_count() + 1);
							if (resultVO.getLogin_fail_count() == 3) {
								resultVO.setLogin_fail_count(0);
								resultVO.setLast_lock_time(new Date());
								try {
									studentService.editStudent(resultVO);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							try {
								studentService.editStudent(resultVO);
							} catch (Exception e) {
								e.printStackTrace();
							}
							redirectAttributes.addFlashAttribute("message", "密码错误");
							return "redirect:/login";
						}
					} else {
						String message = "账号已锁定，请等"
								+ (5 - (new Date().getTime() - resultVO.getLast_lock_time().getTime()) / (1000 * 60))
								+ "分钟解除";// 得到分钟数
						System.out.println(message);
						redirectAttributes.addFlashAttribute("message", message);
						return "redirect:/login";
					}
				} else {
					redirectAttributes.addFlashAttribute("message", "没有输入密码");
					System.out.println("没有输入密码");
					return "redirect:/login";
				}

			} else {
				redirectAttributes.addFlashAttribute("message", "用户不存在");
				System.out.println("用户不存在");
				return "redirect:/login";
			}
		}
		return null;
	}

	// 进行登出
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(RedirectAttributes redirectAttributes, StudentVO vo, HttpSession session) {
		session.setAttribute("examUser", null);
		session.setAttribute("role", null);
		return "redirect:/login";
	}
}
